﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OurMemory
{
    public partial class FormMainHM : Form
    {
        private HappyMemoriesServices service;
        public FormMainHM()
        {
            InitializeComponent();
            service = new HappyMemoriesServices();
          
            panel1.BackgroundImageLayout = ImageLayout.Stretch;

            this.ClientSize = new Size(1200, 800); // 设置客户区大小
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = !dataGridView1.Visible;
            pictureBox7.Visible = !pictureBox7.Visible;
            var lst = service.GetAll();
            this.dataGridView1.DataSource = lst;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            FormCreateHM frm = new FormCreateHM();
            var result = frm.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAll();
                this.dataGridView1.DataSource = lst;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // 获取当前行的id
            if (this.dataGridView1.CurrentRow != null)
            {
                Guid id = (Guid)this.dataGridView1.CurrentRow.Cells["Id"].Value;
                FormEditHM frm = new FormEditHM(id);
                var result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    var lst = service.GetAll();
                    this.dataGridView1.DataSource = lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    cm.Refresh();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // 获取当前行的id
            if (this.dataGridView1.CurrentRow != null)
            {
                Guid id = (Guid)this.dataGridView1.CurrentRow.Cells["Id"].Value;
                string Story = (string)this.dataGridView1.CurrentRow.Cells["Story"].Value;
                var result = MessageBox.Show($"是否删除【{Story}】的信息?",
                                           "系统提示",
                                           MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    //用户确认
                    service.Delete(id);
                    var lst = service.GetAll();
                    this.dataGridView1.DataSource = lst;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
